"""
菜单管理 Schema
"""

from datetime import datetime
from typing import Dict, List, Optional

from pydantic import Field

from app.schemas.base import BaseSchema


class MenuBase(BaseSchema):
    """菜单管理 基础模型"""

    parent_id: int = Field(description="父菜单ID", alias="parentId")
    name: str = Field(max_length=64, description="菜单名称", alias="name")
    type: int = Field(description="菜单类型（1-菜单 2-目录 3-外链 4-按钮）", alias="type")
    tree_path: Optional[str] = Field(default=None, max_length=255, description="父节点ID路径", alias="treePath")
    route_name: Optional[str] = Field(
        default=None, max_length=255, description="路由名称（Vue Router 中用于命名路由）", alias="routeName"
    )
    route_path: Optional[str] = Field(
        default=None, max_length=128, description="路由路径（Vue Router 中定义的 URL 路径）", alias="routePath"
    )
    component: Optional[str] = Field(
        default=None,
        max_length=128,
        description="组件路径（组件页面完整路径，相对于 src/views/，缺省后缀 .vue）",
        alias="component",
    )
    perm: Optional[str] = Field(default=None, max_length=128, description="【按钮】权限标识", alias="perm")
    always_show: Optional[int] = Field(
        default=None, description="【目录】只有一个子路由是否始终显示（1-是 0-否）", alias="alwaysShow"
    )
    keep_alive: Optional[int] = Field(
        default=None, description="【菜单】是否开启页面缓存（1-是 0-否）", alias="keepAlive"
    )
    visible: Optional[int] = Field(default=None, description="显示状态（1-显示 0-隐藏）", alias="visible")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    icon: Optional[str] = Field(default=None, max_length=64, description="菜单图标", alias="icon")
    redirect: Optional[str] = Field(default=None, max_length=128, description="跳转路径", alias="redirect")
    params: Optional[str] = Field(default=None, max_length=255, description="路由参数", alias="params")


class MenuCreate(MenuBase):
    """创建菜单管理 模型"""

    pass


class MenuUpdate(MenuBase):
    """更新菜单管理 模型"""

    parent_id: Optional[int] = Field(default=None, description="父菜单ID", alias="parentId")
    name: Optional[str] = Field(default=None, max_length=64, description="菜单名称", alias="name")
    type: Optional[int] = Field(default=None, description="菜单类型（1-菜单 2-目录 3-外链 4-按钮）", alias="type")
    tree_path: Optional[str] = Field(default=None, max_length=255, description="父节点ID路径", alias="treePath")
    route_name: Optional[str] = Field(
        default=None, max_length=255, description="路由名称（Vue Router 中用于命名路由）", alias="routeName"
    )
    route_path: Optional[str] = Field(
        default=None, max_length=128, description="路由路径（Vue Router 中定义的 URL 路径）", alias="routePath"
    )
    component: Optional[str] = Field(
        default=None,
        max_length=128,
        description="组件路径（组件页面完整路径，相对于 src/views/，缺省后缀 .vue）",
        alias="component",
    )
    perm: Optional[str] = Field(default=None, max_length=128, description="【按钮】权限标识", alias="perm")
    always_show: Optional[int] = Field(
        default=None, description="【目录】只有一个子路由是否始终显示（1-是 0-否）", alias="alwaysShow"
    )
    keep_alive: Optional[int] = Field(
        default=None, description="【菜单】是否开启页面缓存（1-是 0-否）", alias="keepAlive"
    )
    visible: Optional[int] = Field(default=None, description="显示状态（1-显示 0-隐藏）", alias="visible")
    sort: Optional[int] = Field(default=None, description="排序", alias="sort")
    icon: Optional[str] = Field(default=None, max_length=64, description="菜单图标", alias="icon")
    redirect: Optional[str] = Field(default=None, max_length=128, description="跳转路径", alias="redirect")
    params: Optional[str] = Field(default=None, max_length=255, description="路由参数", alias="params")


class MenuPublic(MenuBase):
    """菜单管理 公共模型，用于API响应"""

    id: int = Field(alias="id", description="ID")
    parent_id: int = Field(alias="parentId", description="父菜单ID")
    name: str = Field(alias="name", max_length=64, description="菜单名称")
    type: int = Field(alias="type", description="菜单类型（1-菜单 2-目录 3-外链 4-按钮）")
    tree_path: Optional[str] = Field(default=None, alias="treePath", max_length=255, description="父节点ID路径")
    route_name: Optional[str] = Field(
        default=None, alias="routeName", max_length=255, description="路由名称（Vue Router 中用于命名路由）"
    )
    route_path: Optional[str] = Field(
        default=None, alias="routePath", max_length=128, description="路由路径（Vue Router 中定义的 URL 路径）"
    )
    component: Optional[str] = Field(
        default=None,
        alias="component",
        max_length=128,
        description="组件路径（组件页面完整路径，相对于 src/views/，缺省后缀 .vue）",
    )
    perm: Optional[str] = Field(default=None, alias="perm", max_length=128, description="【按钮】权限标识")
    always_show: Optional[int] = Field(
        default=None, alias="alwaysShow", description="【目录】只有一个子路由是否始终显示（1-是 0-否）"
    )
    keep_alive: Optional[int] = Field(
        default=None, alias="keepAlive", description="【菜单】是否开启页面缓存（1-是 0-否）"
    )
    visible: Optional[int] = Field(default=None, alias="visible", description="显示状态（1-显示 0-隐藏）")
    sort: Optional[int] = Field(default=None, alias="sort", description="排序")
    icon: Optional[str] = Field(default=None, alias="icon", max_length=64, description="菜单图标")
    redirect: Optional[str] = Field(default=None, alias="redirect", max_length=128, description="跳转路径")
    create_time: Optional[datetime] = Field(default=None, alias="createTime", description="创建时间")
    update_time: Optional[datetime] = Field(default=None, alias="updateTime", description="更新时间")
    params: Optional[str] = Field(default=None, alias="params", max_length=255, description="路由参数")


class MenuVO(BaseSchema):
    """菜单视图对象"""

    id: int = Field(alias="id", description="菜单ID")
    parent_id: Optional[int] = Field(None, alias="parentId", description="父菜单ID")
    name: str = Field(alias="name", max_length=64, description="菜单名称")
    type: int = Field(alias="type", description="菜单类型")
    route_name: Optional[str] = Field(default=None, alias="routeName", max_length=255, description="路由名称")
    route_path: Optional[str] = Field(default=None, alias="routePath", max_length=128, description="路由路径")
    component: Optional[str] = Field(
        default=None,
        alias="component",
        max_length=128,
        description="组件路径",
    )
    sort: Optional[int] = Field(default=None, alias="sort", description="菜单排序(数字越小排名越靠前)")
    visible: Optional[int] = Field(default=None, alias="visible", description="菜单是否可见(1:显示;0:隐藏)")
    icon: Optional[str] = Field(default=None, alias="icon", max_length=64, description="ICON")
    redirect: Optional[str] = Field(default=None, alias="redirect", max_length=128, description="跳转路径")
    perm: Optional[str] = Field(default=None, alias="perm", max_length=128, description="按钮权限标识")
    children: Optional[list["MenuVO"]] = Field(default=None, alias="children", description="子菜单")


class RouteMeta(BaseSchema):
    """
    路由属性类型
    """

    title: Optional[str] = Field(default=None, description="路由title", json_schema_extra={"example": "用户管理"})
    icon: Optional[str] = Field(default=None, description="ICON", json_schema_extra={"example": "user"})
    hidden: Optional[bool] = Field(
        default=None, description="是否隐藏(true-是 false-否)", json_schema_extra={"example": "true"}
    )
    keep_alive: Optional[bool] = Field(
        default=None, alias="keepAlive", description="【菜单】是否开启页面缓存", json_schema_extra={"example": "true"}
    )
    always_show: Optional[bool] = Field(
        default=None,
        alias="alwaysShow",
        description="【目录】只有一个子路由是否始终显示",
        json_schema_extra={"example": "true"},
    )
    params: Optional[Dict[str, str]] = Field(default=None, description="路由参数")


class RouteVO(BaseSchema):
    """
    路由对象
    """

    path: Optional[str] = Field(default=None, description="路由路径", json_schema_extra={"example": "user"})
    component: Optional[str] = Field(
        default=None, description="组件路径", json_schema_extra={"example": "system/user/index"}
    )
    redirect: Optional[str] = Field(
        default=None, description="跳转链接", json_schema_extra={"example": "https://www.youlai.tech"}
    )
    name: Optional[str] = Field(default=None, description="路由名称")
    meta: Optional[RouteMeta] = Field(default=None, description="路由属性")
    children: Optional[List["RouteVO"]] = Field(default=None, description="子路由列表")


class MenuForm(BaseSchema):
    """菜单表单对象"""

    id: Optional[int] = Field(default=None, description="菜单ID")
    parent_id: Optional[int] = Field(default=None, alias="parentId", description="父菜单ID")
    name: Optional[str] = Field(default=None, max_length=64, description="菜单名称")
    type: Optional[int] = Field(default=None, description="菜单类型（1-菜单 2-目录 3-外链 4-按钮）")
    route_name: Optional[str] = Field(default=None, alias="routeName", max_length=255, description="路由名称")
    route_path: Optional[str] = Field(default=None, alias="routePath", max_length=128, description="路由路径")
    component: Optional[str] = Field(
        default=None,
        alias="component",
        max_length=128,
        description="组件路径（组件页面完整路径，相对于 src/views/，缺省后缀.vue）",
    )
    perm: Optional[str] = Field(default=None, alias="perm", max_length=128, description="【按钮】权限标识")
    visible: Optional[int] = Field(default=None, ge=0, le=1, alias="visible", description="显示状态（1-显示 0-隐藏）")
    sort: Optional[int] = Field(default=None, alias="sort", description="排序")
    icon: Optional[str] = Field(default=None, alias="icon", max_length=64, description="菜单图标")
    redirect: Optional[str] = Field(default=None, alias="redirect", max_length=128, description="跳转路径")
    keep_alive: Optional[int] = Field(
        default=None, alias="keepAlive", description="【菜单】是否开启页面缓存（1-是 0-否）"
    )
    always_show: Optional[int] = Field(
        default=None, alias="alwaysShow", description="【目录】只有一个子路由是否始终显示（1-是 0-否）"
    )
    params: Optional[List[Dict[str, str]]] = Field(default=None, alias="params", description="路由参数")

    # @field_serializer("id", "parent_id", when_used="json")
    # def id_serializer(self, v: int) -> Optional[str]:
    #     """
    #     序列化ID字段，当ID为0时返回None
    #     """
    #     if v:
    #         return str(v)
    #     return v
